package com.coderman.daotest.dao;

import com.alibaba.fastjson.JSON;
import com.coderman.daoclient.bean.KVPair;
import com.coderman.daoclient.dao.IDao;
import com.coderman.daoclient.dao.impl.IDaoImpl;
import com.coderman.daotest.csdn.bean.CsdnUserEntity;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.StringUtils;

import java.io.File;
import java.sql.SQLException;
import java.util.*;

/**
 * Created by ${fanchunshuai} on 2017-8-2.
 *
 * @author 58
 * @version: V1.0
 * @Desc:
 * @Copyright (c) 2017 58到家-版权所有
 */
public class IDaoImplTest extends IDaoImpl<CsdnUserEntity,Long> {
    static IDaoImpl<CsdnUserEntity,Long> dao = new IDaoImpl<CsdnUserEntity,Long>();
    public static void main(String[] args) {

        try {

            //CsdnUserEntity userEntity = (CsdnUserEntity) dao.getById(CsdnUserEntity.class,50L);

            Map<String,String> map = dao.getMap(CsdnUserEntity.class,"user_name","id");

            File file = new File("E:\\spiderFile\\csdnUserInfo2.txt");

            List<String> list = FileUtils.readLines(file,"UTF-8");
            System.out.println(list.size()+"-----------------------"+map.size());
            dao.beginTransaction();
            list.parallelStream().forEach(str->{
                String [] array = str.split(":");
                String userName = array[1].substring(array[1].lastIndexOf("/")+1);
                String id = map.get(userName);
                if(StringUtils.isNotBlank(id)){
                    List<KVPair> kvPairList = new ArrayList<KVPair>();

                    if(StringUtils.isNotBlank(array[4]) && StringUtils.isNumeric(array[4])){

                        kvPairList.add(KVPair.build("fans",Integer.parseInt(array[4])));
                    }

                    if(StringUtils.isNotBlank(array[3]) && StringUtils.isNumeric(array[3])){
                        kvPairList.add(KVPair.build("guanzhu",Integer.parseInt(array[3])));
                    }

                    kvPairList.add(KVPair.build("nick_name",array[2]));

                    try {
                        if(kvPairList.size() == 3){
                            dao.updateEntity(CsdnUserEntity.class,kvPairList,KVPair.build("id",Long.parseLong(id)));
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
                map.remove(userName);

            });
            dao.commitTransaction();
            System.out.println("-------------------done------------------------------------------");

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
